<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Commerce\Repository\Commerce_merchant;

use Plugin\Attractions\Commerce\Model\Commerce_merchant\CommerceMerchant;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 商户信息 Repository类
 */
class CommerceMerchantRepository extends IRepository
{
   public function __construct(
        protected readonly CommerceMerchant $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 主键ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 商户名称
        if (isset($params['merchant_name']) && !empty($params['merchant_name'])) {
            $query->where('merchant_name', 'like', '%'.$params['merchant_name'].'%');
        }

        // 商户类型：1-个体户，2-企业，3-虚拟商户，4-合作社，5-政府单位
        if (isset($params['merchant_type']) && !empty($params['merchant_type'])) {
            $query->where('merchant_type', '=', $params['merchant_type']);
        }

        // 法人代表
        if (isset($params['legal_person']) && !empty($params['legal_person'])) {
            $query->where('legal_person', 'like', '%'.$params['legal_person'].'%');
        }

        // 法人联系方式
        if (isset($params['legal_person_phone']) && !empty($params['legal_person_phone'])) {
            $query->where('legal_person_phone', 'like', '%'.$params['legal_person_phone'].'%');
        }

        // 法人身份证
        if (isset($params['legal_person_id_card']) && !empty($params['legal_person_id_card'])) {
            $query->where('legal_person_id_card', 'like', '%'.$params['legal_person_id_card'].'%');
        }

        // 商户联系人
        if (isset($params['contact_person']) && !empty($params['contact_person'])) {
            $query->where('contact_person', 'like', '%'.$params['contact_person'].'%');
        }

        // 商户联系电话
        if (isset($params['contact_phone']) && !empty($params['contact_phone'])) {
            $query->where('contact_phone', 'like', '%'.$params['contact_phone'].'%');
        }

        // 联系邮箱
        if (isset($params['contact_email']) && !empty($params['contact_email'])) {
            $query->where('contact_email', 'like', '%'.$params['contact_email'].'%');
        }

        // 商店地址
        if (isset($params['address']) && !empty($params['address'])) {
            $query->where('address', 'like', '%'.$params['address'].'%');
        }

        // 经营范围
        if (isset($params['business_scope']) && !empty($params['business_scope'])) {
            $query->where('business_scope', '=', $params['business_scope']);
        }

        // 商户开门时间
        if (isset($params['open_time']) && !empty($params['open_time']) && is_array($params['open_time']) && count($params['open_time']) == 2) {
            $query->whereBetween(
                'open_time',
                [ $params['open_time'][0], $params['open_time'][1] ]
            );
        }

        // 商户关门时间
        if (isset($params['close_time']) && !empty($params['close_time']) && is_array($params['close_time']) && count($params['close_time']) == 2) {
            $query->whereBetween(
                'close_time',
                [ $params['close_time'][0], $params['close_time'][1] ]
            );
        }

        // 注册资本
        if (isset($params['registered_capital']) && !empty($params['registered_capital'])) {
            $query->where('registered_capital', '=', $params['registered_capital']);
        }

        // 成立日期
        if (isset($params['established_date']) && !empty($params['established_date']) && is_array($params['established_date']) && count($params['established_date']) == 2) {
            $query->whereBetween(
                'established_date',
                [ $params['established_date'][0], $params['established_date'][1] ]
            );
        }

        // 营业执照照片
        if (isset($params['business_license_url']) && !empty($params['business_license_url'])) {
            $query->where('business_license_url', 'like', '%'.$params['business_license_url'].'%');
        }

        // 营业执照号
        if (isset($params['business_license']) && !empty($params['business_license'])) {
            $query->where('business_license', 'like', '%'.$params['business_license'].'%');
        }

        // 税务登记号
        if (isset($params['tax_id']) && !empty($params['tax_id'])) {
            $query->where('tax_id', 'like', '%'.$params['tax_id'].'%');
        }

        // 开户银行
        if (isset($params['bank_name']) && !empty($params['bank_name'])) {
            $query->where('bank_name', 'like', '%'.$params['bank_name'].'%');
        }

        // 银行账号
        if (isset($params['bank_account']) && !empty($params['bank_account'])) {
            $query->where('bank_account', 'like', '%'.$params['bank_account'].'%');
        }

        // 商户状态：1-正常营业，2-歇业，3-注销
        if (isset($params['status']) && !empty($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 备注
        if (isset($params['remark']) && !empty($params['remark'])) {
            $query->where('remark', '=', $params['remark']);
        }

        if (isset($params['admin_id']) && !empty($params['admin_id'])) {
            $query->where('admin_id', '=', $params['admin_id']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 
        if (isset($params['deleted_at']) && !empty($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        return $query;
    }
}